package com.chenlonghai.sbmybatis.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.chenlonghai.sbmybatis.entity.Opinion;
import com.chenlonghai.sbmybatis.entity.ResultModel;
import com.chenlonghai.sbmybatis.service.OpinionService;

@RestController
@RequestMapping("/opinion")
public class OpinionController {
	
   @Autowired OpinionService  opinionService;
	
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public Map<String, Object> listOpinion(Opinion opinion){
		Map<String, Object>modelMap = new HashMap<>();
		List<Opinion>list =  opinionService.queryOpinionByType(opinion);
		
		ResultModel<List<Opinion>>result =new ResultModel<List<Opinion>>(list, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	
	@RequestMapping(value="/alllist",method = RequestMethod.GET)
	public Map<String, Object> alllist_opinion(){
		Map<String, Object>modelMap = new HashMap<>();
		List<Opinion>list =  opinionService.queryOpinion();
		
		ResultModel<List<Opinion>>result =new ResultModel<List<Opinion>>(list, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	
	@RequestMapping(value="/aopinion",method = RequestMethod.GET)
	public Map<String, Object> aopinion(Integer id){
		
		System.out.println(id +"i<-d");
		Map<String, Object>modelMap = new HashMap<>();
		Opinion opinion =  opinionService.queryOpinionById(id);
		
		ResultModel<Opinion>result =new ResultModel<Opinion>(opinion, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	@RequestMapping(value="/deleteById",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> deleteById(Integer id){
		System.out.println("opinion-->"+id);
	    Map<String, Object>modelMap = new HashMap<>();
		boolean isOk =  opinionService.deleteById(id );
		
		if (isOk) {
			ResultModel<String>result =new ResultModel<String>("", modelMap);
			modelMap = result.getResltMap();
			return modelMap;	
		}
		ResultModel<String>result =new ResultModel<String>(null, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	
	@RequestMapping(value="/add",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> addOpionion(
            HttpServletRequest request,Opinion opinion){
		

		
		
		Map<String, Object>modelMap = new HashMap<>();
		 boolean isOk = false;
        System.out.println(opinion.getImgUrl());
        System.out.println(opinion.getLinkUrl());
        System.out.println(opinion.getTypeOne());
        System.out.println(opinion.getTypeTwo());
        System.out.println(opinion.getContent());
        opinion.setCreateTime(new Date());
        opinion.setUpdateTime(new Date());
        isOk =   opinionService.insertOpinion(opinion);
        
        if (isOk) {
        	ResultModel<String>result = new ResultModel<String>("",modelMap);
        	 return  result.getResltMap();
		}
     	ResultModel<String>result = new ResultModel<String>(null,modelMap);
     	
        return result.getResltMap();
        }
	@RequestMapping(value="/update",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> updateOpionion(
            HttpServletRequest request,Opinion opinion){
		

		
		
		Map<String, Object>modelMap = new HashMap<>();
		 boolean isOk = false;
        System.out.println(opinion.getImgUrl());
        System.out.println(opinion.getLinkUrl());
        System.out.println(opinion.getTypeOne());
        System.out.println(opinion.getTypeTwo());
        System.out.println(opinion.getContent());
        opinion.setUpdateTime(new Date());
        isOk =   opinionService.updateOpinion(opinion);
        
        if (isOk) {
        	ResultModel<String>result = new ResultModel<String>("",modelMap);
        	 return  result.getResltMap();
		}
     	ResultModel<String>result = new ResultModel<String>(null,modelMap);
     	
        return result.getResltMap();
        }
	
}
